package opensslCore;
import java.io.BufferedReader;
import java.util.Arrays;

/*
 * @author Arnaud Sansig
 * @info Read data continuously from input stream (reader attribut) and write it to STDOUT
 */
public class PipeReader implements Runnable {

	private BufferedReader reader;                                          /*InputStream*/
	private boolean isRunning;						/*Used to stop the process*/
	/*
	 * @info Default constructor
	 */
	public PipeReader(BufferedReader reader) {
		this.reader=reader;
		this.isRunning=true;
	}

	/*
	 * @info Read data from reader
	 * @info display it on STDOUT
	 * @see java.lang.Runnable#run()
	 */
	@Override
	public void run() {
		try
		{
			/*Read data from reader and display it on STDOUT*/
			while (this.isRunning) {
				char [] cbuf=new char[128];
				int n=this.reader.read(cbuf);
				System.out.println(new String(Arrays.copyOf(cbuf, n)));
			}
		}
		catch(Exception ex)
		{
			System.err.println("Exception PipeReader::run()");
		}
	}
	
	/*
	 * @info Stop the reading process
	 */
	public void stopReading() {
		this.isRunning=false;
	}
}